package com.qiguliuxing.dts.admin.web;

import com.alibaba.fastjson.JSONObject;
import com.qiguliuxing.dts.admin.annotation.RequiresPermissionsDesc;
import com.qiguliuxing.dts.admin.dao.IoralCustomerAll;
import com.qiguliuxing.dts.admin.service.AdminIoralCustomerService;
import com.qiguliuxing.dts.admin.util.AuthSupport;
import com.qiguliuxing.dts.core.util.ResponseUtil;
import com.qiguliuxing.dts.core.validator.Order;
import com.qiguliuxing.dts.core.validator.Sort;
import com.qiguliuxing.dts.db.domain.IoralCustomer;
import com.qiguliuxing.dts.db.domain.ProductProduction;
import com.qiguliuxing.dts.db.service.ProductProductionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@RestController
@RequestMapping("/admin/ioralCustomer")
@Validated
public class AdminIoralCustomerController {

    @Autowired
    private AdminIoralCustomerService adminIoralCustomerService;
    @Autowired
    private ProductProductionService ioralCustomerService;
    private static final Logger logger = LoggerFactory.getLogger(AdminIoralCustomerController.class);

    @RequiresPermissions("admin:ioralCustomer:list")
    @RequiresPermissionsDesc(menu = { "碘131生产发货管理", "用户管理" }, button = "查询")
    @GetMapping("/list")
    public Object list(String userName, String province,String consignee, @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit/*,
                       @Sort @RequestParam(defaultValue = "id") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order*/) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 碘131生产发货管理->查询,请求参数:instrumentNumber:{},instrumentName:{},page:{}", page);



        return adminIoralCustomerService.list(userName,province,consignee,page, limit/*, sort,order*/);
    }


    @GetMapping("/getSortlist")
    public Object getSortlist(String userName, String province,String consignee, @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       @Sort @RequestParam(defaultValue = "id") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 碘131生产发货管理->查询,请求参数:instrumentNumber:{},instrumentName:{},page:{}", page);



        return adminIoralCustomerService.list(userName,province,consignee,page, limit/*, sort,order*/);
    }

/*    @GetMapping("/catAndBrand")
    public Object catAndBrand() {
        return adminInstrumentService.catAndBrand();
    }*/

    /**
     * 编辑商品
     *
     * @param
     * @return
     */
    @RequiresPermissions("admin:ioralCustomer:update")
    @RequiresPermissionsDesc(menu = { "碘131生产发货管理", "客户管理" }, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody IoralCustomer ioralCustomer) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 碘131生产发货管理->客户管理->编辑,请求参数:{}", JSONObject.toJSONString(ioralCustomer.toString()));

        return adminIoralCustomerService.update(ioralCustomer);
    }

    @RequiresPermissions("admin:ioralCustomer:insertMany")
    @RequiresPermissionsDesc(menu = { "碘131生产发货管理", "客户管理" }, button = "导入")
    @GetMapping("/insertMany")
    public boolean addUser(@RequestParam("file") MultipartFile file) {
        boolean a = false;
        String fileName = file.getOriginalFilename();
        try {
            /* a = itestService.batchImport(fileName, file);*/
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  a;
    }

    /**
     * 删除器材
     *
     * @param ioralCustomer
     * @return
     */
 /*   @RequiresPermissions("admin:ioralCustomer:deleteProductProduction")
    @RequiresPermissionsDesc(menu = { "碘131生产发货管理", "客户管理" }, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody ProductProduction ioralCustomer) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 碘131生产发货管理->删除,请求参数:{}", JSONObject.toJSONString(ioralCustomer));

        return adminIoralCustomerService.delete(ioralCustomer);
    }
*/
    /*@RequiresPermissions("admin:instrument:insertMany")
    public Object insertMany(@RequestBody List<InstrumentAll> list){

        return
    }
*/
    /**
     * 添加商品
     *哈哈哈哈哈哈哈哈哈
     * @param 
     * @return
     */
    @RequiresPermissions("admin:ioralCustomer:create")
    @RequiresPermissionsDesc(menu = { "碘131生产发货管理", "客户管理" }, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody IoralCustomer ioralCustomer) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName() + "] 碘131生产发货管理->订单列表->新增,请求参数:{}", JSONObject.toJSONString(ioralCustomer));
        /*IoralCustomer ioralCustomer = ioralCustomerAll.getIoralCustomer();*/


        logger.info(ioralCustomer.toString());
        return adminIoralCustomerService.create(ioralCustomer);
    }

    /**
     * 器材详情
     *
     * @param id
     * @return
     */
    @RequiresPermissions("admin:ioralCustomer:read")
    @RequiresPermissionsDesc(menu = { "碘131生产发货管理", "客户管理" }, button = "详情")
    @GetMapping("/detail")
    public Object detail(@NotNull Integer id) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 碘生产管理->详情,请求参数,id:{}", id);
        logger.info("hehehehheheh");
        return adminIoralCustomerService.detail(id);
    }


   /* @RequiresPermissions("admin:ioralCustomer:read")
    @RequiresPermissionsDesc(menu = { "碘131生产发货管理", "客户管理" }, button = "查看产品信息")
    @GetMapping("/info")
    public Object detail(@NotNull String batchNumber) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 碘生产管理->详情,请求参数,id:{}", batchNumber);
        logger.info("根据产品信息查找产品");
        return adminIoralCustomerService.detail(batchNumber);
    }*/

    /*@RequiresPermissions("admin:ioralCustomer:read")*/
    /*@RequiresPermissionsDesc(menu = { "碘131生产发货管理", "客户管理" }, button = "查看产品信息")*/
    @PostMapping("/queryBatchNumbers")
    public Object getBatchNumbers(@RequestParam(defaultValue = "2024-01-01") String startTime, @RequestParam(defaultValue = "2024-12-01")String endTime,String batchNumber) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 碘生产管理->详情,请求参数,id:{}", startTime+endTime);
        logger.info("查找所有产品批号");
        Map<String,Object> data=new HashMap<>();
        Map<String,String> batchNumbers2=new TreeMap<>();
        Map<String,String> batchNumbers3=new TreeMap<>();
        Object batchNumbers=null;
        List<ProductProduction> ioralCustomers=ioralCustomerService.querySelective(startTime,endTime,batchNumber,1,20,"desc","production_date");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for(ProductProduction production:ioralCustomers){
            if(production.getIsEnd()==Boolean.FALSE) {
                String bn = production.getBatchNumber();
                batchNumbers2.put(bn,dateFormat.format(Date.valueOf(bn)));
            }
        }
        batchNumbers2.entrySet().stream()
                .sorted(Map.Entry.comparingByValue())

               /* .forEach(entry ->
                        *//*System.out.println(dateFormat.format(entry.getKey()) + ": " + entry.getValue())*//*
                )*/;
       /* batchNumbers=batchNumbers2.stream()
                .collect(Collectors.groupingBy(s -> s.substring(0, 10)))
                .entrySet()
                .stream()
                .map(entry -> {
                    Map<String, Object> group = Map.of(
                            "label", entry.getKey().substring(0,10),
                            "options", entry.getValue()*//*.stream().map(option -> Map.of("value", option, "label", option)).collect(Collectors.toList())*//*
                    );
                    return group;
                })
                .collect(Collectors.toList());*/
        data.put("batchNumbers",batchNumbers2);
        /*logger.info(batchNumbers.toString());*/
        return ResponseUtil.ok(data);
    }

    @PostMapping("/queryBatchNumbers2")
    public Object getBatchNumbers2(@RequestParam(defaultValue = "2024-01-01") String startTime, @RequestParam(defaultValue = "2024-12-01")String endTime,String batchNumber) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 碘生产管理->详情,请求参数,id:{}", startTime+endTime);
        logger.info("查找所有产品批号");
        Map<String,Object> data=new HashMap<>();
        Map<String,String> batchNumbers2=new TreeMap<>();
        Map<String,String> batchNumbers3=new TreeMap<>();
        Object batchNumbers=null;
        List<ProductProduction> ioralCustomers=ioralCustomerService.querySelective(startTime,endTime,batchNumber,1,20,"desc","production_date");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for(ProductProduction production:ioralCustomers){

                String bn = production.getBatchNumber();
                batchNumbers2.put(bn,dateFormat.format(Date.valueOf(bn)));

        }
        batchNumbers2.entrySet().stream()
                .sorted(Map.Entry.comparingByValue())

        /* .forEach(entry ->
         *//*System.out.println(dateFormat.format(entry.getKey()) + ": " + entry.getValue())*//*
                )*/;
       /* batchNumbers=batchNumbers2.stream()
                .collect(Collectors.groupingBy(s -> s.substring(0, 10)))
                .entrySet()
                .stream()
                .map(entry -> {
                    Map<String, Object> group = Map.of(
                            "label", entry.getKey().substring(0,10),
                            "options", entry.getValue()*//*.stream().map(option -> Map.of("value", option, "label", option)).collect(Collectors.toList())*//*
                    );
                    return group;
                })
                .collect(Collectors.toList());*/
        data.put("batchNumbers",batchNumbers2);
        /*logger.info(batchNumbers.toString());*/
        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:ioralCustomer:read")
    @RequiresPermissionsDesc(menu = { "碘131生产发货管理", "客户管理" }, button = "详情")
    @GetMapping("/more")
    public Object detail2(@NotNull Integer id) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 碘生详情,请求参数,id:{}", id);
        logger.info("heheh胜多负少h");
        return adminIoralCustomerService.detail(id);
    }


    @GetMapping("/getCustomerNames")
    public Object getCustomerNames() {
        List<String> customerNames=adminIoralCustomerService.getNames();
        logger.info("获取用户名列表");
        Map<String,Object> data=new HashMap<>();
        data.put("items",customerNames);
        Integer total=customerNames.size();
        data.put("total",total);
        return ResponseUtil.ok(data);
    }
    /**
     * 删除生产信息
     *
     * @return
     */
    @RequiresPermissions("admin:ioralCustomer:deleteCustomer")
    @RequiresPermissionsDesc(menu = { "碘131生产发货管理", "客户管理" }, button = "删除")
    @PostMapping("/deleteCustomer")
    public Object delete(@RequestBody IoralCustomer ioralCustomer) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 器材管理->器材管理->删除,请求参数:{}", JSONObject.toJSONString(ioralCustomer));

        return adminIoralCustomerService.delete(ioralCustomer);
    }

/*    @RequiresPermissions("admin:ioralCustomer:read")
    @RequiresPermissionsDesc(menu = { "碘131生产发货管理", "客户管理" }, button = "详情")
    @GetMapping("/more")
    public Object detail2(@NotNull Integer id) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 碘生详情,请求参数,id:{}", id);
        logger.info("heheh胜多负少h");
        return adminIoralCustomerService.detail(id);
    }*/


}
